<template>
    <div class="login">
        <div class="top" @click="$router.push('/home')"><img
                :src="require(`../../assets/theme/dark/image/Union.png`)" alt="" /></div>
        <div class="title textColor">{{ $t('登录') }}</div>
        <div class="flex login-tab">
            <div class="textColor1" :class="activeIndex == 0 ? 'active' : ''" @click="changeIndex(0)">{{ $t('账号') }}
            </div>
            <div class="textColor1" :class="activeIndex == 1 ? 'active' : ''" @click="changeIndex(1)">{{ $t('邮箱') }}
            </div>
            <!-- <div class="textColor1" :class="activeIndex == 2 ? 'active' : ''" @click="changeIndex(2)">{{ $t('手机号') }}</div> -->
        </div>
        <ExInput :label="getRegType(activeIndex, true)" :placeholderText="getRegType(activeIndex, false)"
            v-model="username" :dialCode="dialCode" @selectArea="onSelectArea" :area="isArea" :icon="icon" />
        <ExInput style="padding-bottom:0!important;" :label="$t('密码')" :placeholderText="$t('请输入您的密码')"
            v-model="password" typeText="password" />
        <div class="forget" @click="$router.push('/forget')">{{ $t('忘记密码?') }}</div>
        <div class="btn" @click="verifyLogin">{{ $t('登录') }}</div>
        <div class="noTips" style="text-align: center;color:#fff">{{ $t('还没有账号') }}<span
                style="font-weight: bold;color: #fff;text-decoration: underline;" @click="$router.push('/register')">
                {{ $t('去注册') }}</span>
        </div>
        <nationality-list ref='controlChild' :title="$t('选择区域码')" @getName="getName(arguments)"></nationality-list>
    </div>
</template>

<script>
import ExInput from "@/components/ex-input";
import Axios from "@/API/login.js";
import { mapActions, mapGetters } from 'vuex';
import { GET_USERINFO, SET_CONFIG, SET_KEFU, SET_USERINFO } from '@/store/const.store';
import nationalityList from '../authentication/components/nationalityList.vue'
import { encryptText } from "@/utils/aes"
export default {
    props: {

    },
    components: {
        ExInput,
        nationalityList
    },
    data() {
        return {
            username: '',
            password: "",
            activeIndex: 0,
            isArea: false,
            typeText: "password",
            dialCode: 0, //电话号前缀
            icon: '',
            verifcode_type: "", //1 手机验证，2 邮箱验证，3 谷歌验证
            account: "", //用户已绑定的手机号或者邮箱账号
        }
    },
    computed: {
        ...mapGetters({
            theme: 'home/theme'
        }),
    },
    methods: {
        ...mapActions('user', [GET_USERINFO, SET_CONFIG]),
        getRegType(activeIndex, bFlag) {
            switch (activeIndex) {
                case 0:
                    return bFlag ? this.$t('账号') : this.$t('请输入账号');
                case 1:
                    return bFlag ? this.$t('邮箱') : this.$t('请输入您的邮箱');
                case 2:
                    return bFlag ? this.$t('手机号') : this.$t('请输入手机号');
            }
        },
        onSelectArea() {
            this.openBtn();
        },
        //打开国家列表底部弹窗
        openBtn() {
            this.$refs.controlChild.open();
        },
        //获取到当前选中国家的code值
        getName(params) {
            this.icon = params[1];
            this.dialCode = params[2];
        },
        changeIndex(index) {
            this.activeIndex = index;
            switch (index) {
                case 0:
                case 1: {
                    this.isArea = false;
                    break;
                }
                case 2: {
                    this.isArea = true;
                    break;
                }
            }
        },
        verifyLogin() {
            if (this.username == '') {
                switch (this.activeIndex) {
                    case 0:
                        {
                            this.$toast(this.$t('请输入账号'));
                            break;
                        }
                    case 1:
                        {
                            this.$toast(this.$t('请输入邮箱'));
                            break;
                        }
                    case 2:
                        {
                            this.$toast(this.$t('请输入手机号'));
                            break;
                        }
                }
                return false
            }
            if (this.password == '') {
                this.$toast(this.$t('请输入密码'));
                return false
            }
            this.login()
        },
        async login() {
            Axios.googleLogin({
                username: (this.activeIndex == 0 || this.activeIndex == 1) ? this.username : `${this.dialCode}${this.username}`,
                password: this.password,
                // re_password: this.repassword,
                // type: this.type,
                // usercode: this.invitCode,
            }).then((res) => {
                //开启设备锁时，会返回verifcode_types数组,数组中包含account和verifcode_type数据,存储verifcode_types数据,换设备就要验证
                let newdata = JSON.stringify(res.data);
                localStorage.setItem('verifyData', newdata);
                //关掉设备锁时，会返回verifcode_type，account两个字符串，如果用户有绑定谷歌，邮箱，手机，需要验证登录。
                this.verifcode_type = res.data.verifcode_type;
                this.account = res.data.account;

                //未绑定谷歌，手机，邮箱情况下，直接登录
                if (res.data.token) {
                    this.GET_USERINFO(res.data)
                    this.SET_CONFIG() //获取判断是否乘以杠杆字段
                    this.$router.push('/home')
                } else {
                    //绑定谷歌、手机，邮箱验证情况下，需跳转验证页面，重新调新的接口
                    this.$router.push({
                        name: "vertifyCode",
                        params: {
                            username: (this.activeIndex == 0 || this.activeIndex == 1) ? this.username : `${this.dialCode}${this.username}`,
                            // password: this.password,
                            type: this.verifcode_type,
                            account: this.account
                        }
                    });
                }
                this.username = "";
                this.password = "";
            }).catch((error) => {
                if (error.code === 'ECONNABORTED') { this.$toast(this.$t('网络超时！')); }
                else if (error.code === '298') {
                    // this.$toast(this.$t('您的账户存在风险,请进行安全绑定。'));
                    this.$store.commit('user/SET_USERINFO', { token: error.data.token })
                    this.$router.replace({ name: 'bindVerify', params: { type: 3, isSafeLogin: true } })
                }

                else if (error.msg !== undefined) { this.$toast(this.$t(error.msg)); }
            });
        }
    }
}
</script>

<style lang="scss" scoped>
.login {
    width: 100%;
    padding: 30px;
    font-size: 26px;
    box-sizing: border-box;
}

.top {
    padding-left: 18px;
    padding-top: 18px;

    img {
        width: 37px;
        height: 37px;
    }
}

.title {
    font-weight: 700;
    font-size: 52px;
    margin-top: 54px;
    margin-bottom: 66px;
}

.login-tab {
    margin-bottom: 40px;

    div {
        padding: 0 40px;
        height: 68px;
        line-height: 68px;
        text-align: center;
        border-radius: 8px;
        margin-right: 20px;
    }

    .active {
        @include themify() {
            background: themed("tab_background");
        }

        @include themify() {
            color: themed("text_color");
        }
    }
}

.forget {
    font-size: 24px;
    line-height: 28px;
    margin-top: 18px;
    text-align: right;
    color: #fff !important;
}

.btn {
    color: #fff;
    height: 88px;
    line-height: 88px;
    text-align: center;
    font-size: 35px;
    margin-top: 36px;
    border-radius: 10px;
    background-color: #000000;
}

.noTips {
    margin-top: 44px;
}
::v-deep .iptbox input {
  background: #121212;
}
</style>
